
// 多媒体工具ffmpeg下载 http://ffmpeg.p2hp.com/download.html
// 图片压缩工具pngquant下载 http://pngquant.com/
// const path = require('path');
// const fs = require('fs')
// const util = require('util')
// const {execSync, exec } = require('child_process')
import path from 'path';
import fs from 'fs'
import util from 'util'
import { exec,execSync } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// var os = require("node:os")
// console.log(os.arch());
// console.log(__dirname);
// console.log('=========process', process.argv)


// 测试同步转promise函数
const promisifyTest = () => {
  const execPromise = util.promisify(exec) // 转异步函数
  execPromise('node -v').then(res=>{
    console.log(res,'res')
  }).catch(err=>{
      console.log(err,'err')
  })
}

// 测试ffmpeg多媒体处理
const ffmpegTest = () => {
  const path1 = path.join(__dirname, '/1.mp4')
  const path2 = path.join(__dirname, '/1.gif')
  execSync(`ffmpeg -i ${path1} ${path2}`,{stdio:'inherit'})
}

// 测试pngquant图片压缩
const pngquantTest = () => { 
  const path1 = path.join(__dirname, '/images/test.png')
  const path2 = path.join(__dirname, 'images', 'output.png')
  if(fs.existsSync(path2)) fs.unlinkSync(path2);
  // quality 图片质量 0-100 越大质量越好
  execSync(`pngquant ${path1} --quality=82 --output ${path2}`, null,  (err, stdout, stderr) => {
    console.log('==========err', err)
    console.log('==========stdout', stdout)
    console.log('==========stderr', stderr)
  })
}
